<template>
  <el-card>
    <div slot="header" style="line-height: 40px">
      <h1>添加管理员</h1>
    </div>
    <el-form
      ref="categoryForm"
      :rules="rules"
      label-width="80px"
      :model="categoryForm"
    >
      <el-form-item label="序号" v-show="false">
        <el-input v-model="categoryForm.id" disabled></el-input>
      </el-form-item>
      <el-form-item label="产品分类" prop="cgname">
        <el-input v-model.trim="categoryForm.cgname"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">保存</el-button>
        <el-button @click="onReset">重置</el-button>
        <el-button @click="$router.back()">返回</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
import categoryApi from '@/http/categoryApi';

export default {
  data() {
    return {
      categoryForm: {
        id:'',
        cgname:''
      },
      rules: {
        cgname: [
          { required: true, message: '请输入产品类名', trigger: 'blur' },
        ],
      },
    };
  },
  created() {
    this.getEditObj();
  },
  methods: {
    onSubmit() {
      this.$refs['categoryForm'].validate(valid => {
        if (valid) {
          console.log('submit!', this.categoryForm);
          categoryApi.save(this.categoryForm).then(res => {
            console.log(res);
            if (res.data.code == 200) {
              this.$router.replace('/goods/category.html');
            } else {
              this.$message({
                showClose: true,
                message: res.data.msg,
                type: 'error',
              });
            }
          });
        } else {
          console.log('error submit!!');
          return false; //阻止表单提交
        }
      });
    },
    onReset() {
      this.$refs['categoryForm'].resetFields();
    },
    getEditObj() {
      let id = this.$route.params.id;
      if (id) {
        categoryApi.edit(id).then(res => {
          if (res.data.code == 200) {
            this.categoryForm = res.data.data;
          }
        });
      }
    },
  },
};
</script>

<style></style>
